package com.wangrui.controller;


import com.wangrui.pojo.Role;
import com.wangrui.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王锐
 * @since 2020-07-05
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/roleList")
    public String RoleListHtml()  {
        return "role/roleList";
    }
    @RequestMapping("/save")
    public String saveHtml()  {
        return "role/save";
    }
    @RequestMapping("/update")
    public String updateHtml()  {
        return "role/update";
    }

    @RequestMapping("/setPermission")
    public String setPermissionHtml()  {
        return "role/setPermission";
    }

    @RequestMapping("/queryAllRoles")
    public ResponseEntity<Map<String, Object>> QueryAllRoles(
            @RequestParam("page")int start,
            @RequestParam("rows")int end
    ) {
        Map<String, Object> stringObjectMap = roleService.queryAllRoles(start, end);
        return ResponseEntity.ok(stringObjectMap);
    }

    @RequestMapping("/roleSave")
    public ResponseEntity<Void> RoleSave(Role role)  {
        roleService.roleSave(role);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @RequestMapping("/roleUpdate")
    public ResponseEntity<Void> roleUpdate(Role role)  {
        roleService.roleUpdate(role);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @RequestMapping("/roleDelete")
    @ResponseBody
    public String roleDelete(int[]  ids)  {
        for (int id : ids) {
            roleService.roleDelete(id);
        }
        return "ok";
    }

}

